//
//  ShopTypeContractViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/27.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "ShopTypeContractViewController.h"
#import "HttpContractAction.h"
#import "ShopTypeTableViewCell.h"
#import "ShopTypeDetailViewController.h"

@interface ShopTypeContractViewController (){
    int offset;
}
@property (nonatomic,strong) NSString *type;
@property (nonatomic,strong) NSString *titleNameString;
@property (nonatomic,strong) NSMutableArray *listArray;

@end

@implementation ShopTypeContractViewController

-(id)initWithType:(NSString*)type withName:(NSString*)name{
    if (self == [super init]) {
        self.type = type;
        self.titleNameString = name;
        self.listArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = 0;
    self.titleName = self.titleNameString;
    
    __weak ShopTypeContractViewController *filterView = self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [filterView loadData:YES];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
    
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadFooter)];
    [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [filterView loadData:NO];

    }];
    
}

-(void)loadData:(BOOL)flag{
    if (flag) {
        offset = 0;
        self.listArray = [NSMutableArray array];
    }else{
        offset++;
    }
    ContractActionInfo *contractActionInfo = [[ContractActionInfo alloc] init];
    contractActionInfo.limit = @"10";
    contractActionInfo.offset = [NSString stringWithFormat:@"%d",offset];
    contractActionInfo.type = self.type;
//    [self showWaitingViewWithMessage:@"加载中..." style:WaitingViewShowStyleFullSelfView];
    [HttpContractAction postContractListWithAction:contractActionInfo block:^(id result, NSError *error) {
//        [self dismissWaitingView];
        if (!error) {
            NSArray *array = result;
            [self.listArray addObjectsFromArray:array];
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            if (array.count<10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    ShopTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ShopTypeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    MerchantContractInfo *merchantContractInfo = self.listArray[indexPath.row];
    [cell setShopTypeCell:merchantContractInfo];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MerchantContractInfo *merchantContractInfo = self.listArray[indexPath.row];
    ShopTypeDetailViewController *shopTypeDetailViewController = [[ShopTypeDetailViewController alloc] initWithShopTypeInfo:merchantContractInfo Block:^{
        [self loadData:YES];
    }];
    [self.navigationController pushViewController:shopTypeDetailViewController animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 115;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
